#include <stdafx.h>
#include "ScriptEditorSolutionSettings.h"
#include "ScriptEditorSolution.h"
#include "ScriptEditorCompiler.h"
#include "LitScriptEngine.h"

namespace Lit
{
	namespace Editor
	{
		namespace SolutionSettingsWidgets
		{
			SolutionWidget::SolutionWidget( QWidget * parent ) 
				: QGroupBox(parent)
			{
				mLayout = new QFormLayout( this );
				setLayout( mLayout );

				setTitle( "Solution Properties" );
				setAlignment( Qt::AlignHCenter );
				
				mLayout->addRow( "Name", (mName = new QLineEdit( this )) );
				mLayout->addRow( "Entry Point", (mEPoint = new QLineEdit( this )) );
				mLayout->addRow( "Rendering Entry Point", (mREPoint = new QLineEdit( this )) );
			}
			SolutionWidget::~SolutionWidget(){}
			void SolutionWidget::prepare( ScriptSolutionPtr sol )
			{
				mName->setText( sol->getName().c_str() );
				mEPoint->setText( sol->getEntryPoint().c_str() );
			}

			SolutionIncludeWidget::SolutionIncludeWidget( QWidget * parent ) 
				: QGroupBox(parent)
			{
				mLayout = new QFormLayout( this );
				setLayout( mLayout );

				setTitle( "Solution Include Path List" );
				setAlignment( Qt::AlignHCenter );
				
				mLayout->addWidget( (mIncludes = new QListWidget( this )) );
			}
			SolutionIncludeWidget::~SolutionIncludeWidget(){}
				
			void SolutionIncludeWidget::prepare( ScriptSolutionPtr sol )
			{
				mIncludes->clear();
				ScriptEngine::FilePathVector vec = sol->getCompiler()->getEngine()->getIncludeSearchPath();
				for( auto i=vec.begin(); i!=vec.end(); i++ )
				{
					mIncludes->addItem( new QListWidgetItem( i->str().c_str() ) );
				}
			}
			AngelWidget::AngelWidget( QWidget * parent ) : QGroupBox(parent){
				mLayout = new QFormLayout( this );
				setLayout( mLayout );

				setTitle( "AngelScript Properties" );
				setAlignment( Qt::AlignHCenter );

				mLayout->addRow( "Name", (mName = new QLineEdit( this )) );
			}
			AngelWidget::~AngelWidget(){}
				
			EditorWidget::EditorWidget( QWidget * parent ) : QGroupBox(parent){
				mLayout = new QFormLayout( this );
				setLayout( mLayout );

				setTitle( "Editor Properties" );
				setAlignment( Qt::AlignHCenter );

				mLayout->addRow( "Name", (mName = new QLineEdit( this )) );
			}
			EditorWidget::~EditorWidget(){}

			EngineWidget::EngineWidget( QWidget * parent ) : QGroupBox(parent){
				mLayout = new QFormLayout( this );
				setLayout( mLayout );

				setTitle( "Engine Properties" );
				setAlignment( Qt::AlignHCenter );
				
				mLayout->addWidget( (mTabs = new QTabWidget( this )) );
				mLayout->addRow( "Name", (mName = new QLineEdit( this )) );
				
				mName->setReadOnly(true);
				mGlobalTable = new QTableWidget(mTabs);
				mTabs->addTab( mGlobalTable, "Globals" );
				
				mObjectTypes = new QTableWidget(mTabs);
				mTabs->addTab( mObjectTypes, "Object Types" );
				mEnumTable = new QTableWidget(mTabs);
				mTabs->addTab( mEnumTable, "Enum Types" );
				
				mGlobalTable->setColumnCount( 3 );
				mGlobalTable->setHorizontalHeaderLabels( QStringList() << "Name" << "Namespace" << "Type" );
				mGlobalTable->verticalHeader()->setVisible( false );
				mGlobalTable->horizontalHeader()->setStretchLastSection( true );
				
				mObjectTypes->setColumnCount( 2 );
				mObjectTypes->setHorizontalHeaderLabels( QStringList() << "Type" << "Namespace" );
				mObjectTypes->verticalHeader()->setVisible( false );
				mObjectTypes->horizontalHeader()->setStretchLastSection( true );

				mEnumTable->setColumnCount( 4 );
				mEnumTable->setHorizontalHeaderLabels( QStringList() << "Name" << "Namespace" << "Decl" << "" );
				mEnumTable->verticalHeader()->setVisible( false );
				mEnumTable->horizontalHeader()->setStretchLastSection( true );
			}

			EngineWidget::~EngineWidget(){}

			void EngineWidget::prepare( ScriptEnginePtr sol )
			{
				size_t numTypes = sol->data()->GetObjectTypeCount();
				mObjectTypes->setRowCount( numTypes );

				for( size_t i=0; i<numTypes; i++ )
				{
					asIObjectType* obj = sol->data()->GetObjectTypeByIndex( i );
					if( obj )
					{
						mObjectTypes->setItem( i, 0, new QTableWidgetItem( obj->GetName() ) );
						mObjectTypes->setItem( i, 1, new QTableWidgetItem( obj->GetNamespace() ) );
					}
					else
					{
						mObjectTypes->setItem( i, 0, new QTableWidgetItem( "???" ) );
						mObjectTypes->setItem( i, 1, new QTableWidgetItem( "???" ) );
					}
				}

				size_t numProps = sol->data()->GetGlobalPropertyCount();
				mGlobalTable->setRowCount( numProps );

				for( size_t i=0; i<numProps; i++ )
				{
					const char * nam;
					const char * namsp;
					int tid = 0;
					assert( sol->data()->GetGlobalPropertyByIndex( i, &nam, &namsp, &tid ) >= 0 );
					asIObjectType * obj = sol->data()->GetObjectTypeById( tid );
					mGlobalTable->setItem( i, 0, new QTableWidgetItem( nam ) );
					mGlobalTable->setItem( i, 1, new QTableWidgetItem( namsp ) );
					if( obj ) mGlobalTable->setItem( i, 2, new QTableWidgetItem( obj->GetName() ) );
					else  mGlobalTable->setItem( i, 2, new QTableWidgetItem( "???" ) );
				}

				size_t numEnums = sol->data()->GetEnumCount();
				mEnumTable->setRowCount( numEnums );

				for( size_t i=0; i<numEnums; i++ )
				{
					const char * nam;
					const char * namsp;
					int tid = 0;
					nam = sol->data()->GetEnumByIndex( i, &tid, &namsp );
					mEnumTable->setItem( i, 0, new QTableWidgetItem( nam ) );
					mEnumTable->setItem( i, 1, new QTableWidgetItem( namsp ) );
					mEnumTable->setItem( i, 2, new QTableWidgetItem( sol->data()->GetTypeDeclaration( tid, true ) ) );
					QStringList lst;
					for( size_t j=0; j<sol->data()->GetEnumValueCount(tid); j++ ){int intt;lst << QString(sol->data()->GetEnumValueByIndex( tid, j, &intt ));}
					QComboBox * cb( new QComboBox( mEnumTable ) );
					cb->addItems( lst );
					mEnumTable->setCellWidget( i, 3, cb );
				}
			}

		};
		ScriptSolutionSettings::ScriptSolutionSettings ( QWidget*parent ) : QDialog(parent)
		{ 
			ui.setupUi(this); 
			ui.treeWidget->clear();
			mAngelNode = new QTreeWidgetItem( ui.treeWidget, QStringList() << "AngelScript" );
			mSolutionNode = new QTreeWidgetItem( ui.treeWidget, QStringList() << "Solution" );
			mEngineNode = new QTreeWidgetItem( ui.treeWidget, QStringList() << "Engine" );
			mEditorNode = new QTreeWidgetItem( ui.treeWidget, QStringList() << "Editor" );
			
			mSolutionIncludesNode = new QTreeWidgetItem( mSolutionNode, QStringList() << "Includes" );

			mAngelWidget = new SolutionSettingsWidgets::AngelWidget( ui.stackedWidget );
			mSolutionWidget=new SolutionSettingsWidgets::SolutionWidget( ui.stackedWidget );
			mSolutionIncludeWidget=new SolutionSettingsWidgets::SolutionIncludeWidget( ui.stackedWidget );
			mEditorWidget = new SolutionSettingsWidgets::EditorWidget( ui.stackedWidget ) ;
			mEngineWidget = new SolutionSettingsWidgets::EngineWidget( ui.stackedWidget );
			
			ui.stackedWidget->addWidget( mAngelWidget );
			ui.stackedWidget->addWidget( mSolutionWidget );
			ui.stackedWidget->addWidget( mSolutionIncludeWidget );
			ui.stackedWidget->addWidget( mEditorWidget );
			ui.stackedWidget->addWidget( mEngineWidget );

			ui.treeWidget->addTopLevelItem(mAngelNode);
			ui.treeWidget->addTopLevelItem(mSolutionNode);
			ui.treeWidget->addTopLevelItem(mEditorNode);
			ui.treeWidget->addTopLevelItem(mEngineNode);

			mSolutionNode->addChild( mSolutionIncludesNode );
			mSolutionIncludesNode->setExpanded( true );
			mSolutionNode->setExpanded( true );

			connect( ui.treeWidget, SIGNAL(itemClicked(QTreeWidgetItem*,int)), this, SLOT(updateTriggered(QTreeWidgetItem*,int)) );
			mAngelNode->setSelected( true );
			ui.stackedWidget->setCurrentWidget( mAngelWidget );

		}
		ScriptSolutionSettings::~ScriptSolutionSettings( ){}
			
		int ScriptSolutionSettings::exec( ScriptSolutionPtr sol )
		{
			mEngineWidget->prepare( sol->getCompiler()->getEngine() );
			mSolutionWidget->prepare( sol );
			mSolutionIncludesNode->setExpanded( true );

			int ret = QDialog::exec();
			if( ret == QDialog::Accepted )
			{
				sol->setEntryPoint( mSolutionWidget->getEntryPoint()->text().toAscii().data() );
				sol->setRenderEntryPoint( mSolutionWidget->getRenderEntryPoint()->text().toAscii().data() );
			}
			else
			{
			}
			return ret;
		}

		void ScriptSolutionSettings::updateTriggered(QTreeWidgetItem*snd,int)
		{
			if( snd == mAngelNode ) ui.stackedWidget->setCurrentWidget( mAngelWidget );
			else if( snd == mSolutionNode ) ui.stackedWidget->setCurrentWidget( mSolutionWidget );
			else if( snd == mSolutionIncludesNode ) ui.stackedWidget->setCurrentWidget( mSolutionIncludeWidget );
			else if( snd == mEditorNode ) ui.stackedWidget->setCurrentWidget( mEditorWidget );
			else if( snd == mEngineNode ) ui.stackedWidget->setCurrentWidget( mEngineWidget );
		}
	};
};